package com.smartrobot.smartrobot.adapter;

import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.nostra13.universalimageloader.core.ImageLoader;
import com.smartrobot.smartrobot.Common.Common;
import com.smartrobot.smartrobot.activity.NewsDetailActivity;
import com.smartrobot.smartrobot.R;
import com.smartrobot.smartrobot.logToSdCard.LogToSd;
import com.smartrobot.smartrobot.util.FileUtil;
import com.smartrobot.smartrobot.util.ImageLoaderApi;
import com.smartrobot.smartrobot.util.News;
import com.smartrobot.smartrobot.util.User;
import com.smartrobot.smartrobot.view.ExListView;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2014/12/23.
 */
public class NewsListViewAdapter extends BaseAdapter implements AbsListView.OnScrollListener,AdapterView.OnItemClickListener{


    private Context context;
    private List<News> list;
    private ExListView listView;
    private ImageLoader imageLoader;
    private boolean isScroll = false;
    private ViewHolder holder;


    public NewsListViewAdapter(Context context,ExListView listView){

        this.context = context;
        this.listView = listView;
        ImageLoaderApi.initImageLoaderApiInformationPic(context);
        imageLoader = ImageLoader.getInstance();
        list = new ArrayList<News>();
//        list.add(new News("1","春天剪发","春天是一个春暖花开的季节","http://f.hiphotos.baidu.com/image/pic/item/b8014a90f603738d34235f52b01bb051f819ec51.jpg","http://f.hiphotos.baidu.com/image/pic/item/b8014a90f603738d34235f52b01bb051f819ec51.jpg","2015-1-13"));
//        list.add(new News("1","夏天剪发","夏天是一个热情似火的季节","http://e.hiphotos.baidu.com/image/pic/item/29381f30e924b8993d3ccaa66c061d950a7bf628.jpg","http://e.hiphotos.baidu.com/image/pic/item/29381f30e924b8993d3ccaa66c061d950a7bf628.jpg","2015-1-13"));
//        list.add(new News("1","秋天剪发","秋天是一个果实累累的季节","http://c.hiphotos.baidu.com/image/pic/item/8cb1cb1349540923570043e69058d109b3de4938.jpg","http://c.hiphotos.baidu.com/image/pic/item/8cb1cb1349540923570043e69058d109b3de4938.jpg","2015-1-13"));
//        list.add(new News("1","冬天剪发","冬天是一个被窝里的季节","http://pic.yesky.com/imagelist/11/08/12216952_9898.jpg","http://pic.yesky.com/imagelist/11/08/12216952_9898.jpg","2015-1-13"));



    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int i) {
        return i;
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup viewGroup) {

        if(convertView == null){
            //新建一个holder
            convertView =  LayoutInflater.from(context).inflate(R.layout.page_fashion_adapter_item, viewGroup, false);
            holder = new ViewHolder();

            holder.title = (TextView) convertView.findViewById(R.id.fashion_adapter_title);
            holder.simpleContent = (TextView)convertView.findViewById(R.id.fashion_adapter_simpleContent);
            holder.seeAll = (TextView)convertView.findViewById(R.id.fashion_adapter_seeDetail);
            holder.simplePic = (ImageView)convertView.findViewById(R.id.fashion_adapter_simplePic);

            convertView.setTag(holder);

        }else{
            holder = (ViewHolder)convertView.getTag();
        }

        //初始化所有view
        final News news = list.get(position);
        holder.title.setText(news.getTitle());
        holder.simpleContent.setText(news.getSimpleContent());
        imageLoader.displayImage(Common.baseUrl + news.getSimpleImg(),holder.simplePic,ImageLoaderApi.getActivePicOptions());
        Log.e("=====图片路径==================>", "  "+ Common.baseUrl + news.getSimpleImg());


        return convertView;
    }

    @Override
    public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) {

        position = position - 1;
        News news = list.get(position);

        Intent intent = new Intent();
        intent.putExtra("id",news.getId());
        intent.putExtra("title",news.getTitle());
        intent.setClass(context, NewsDetailActivity.class);
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        context.startActivity(intent);

    }


    class ViewHolder{

        TextView title,simpleContent,seeAll;
        ImageView simplePic;


    }

    public void notiNewDataChange(List<News> list){

        if(list.size() > 10){//若是返回动态多余10条就改变消息 这里获取回来绝对是没有的，因为是根据时间排序的
            //引用会改变内部值
            this.list.clear();
            this.list.addAll(list);
            notifyDataSetChanged();
        }else{//若是少于十条就添加到新的里面去
            list.addAll(this.list);
            this.list.clear();
            this.list.addAll(list);
            notifyDataSetChanged();
        }


    }

    public void notiOldDataChange(List<News> list){

        //通知改变数据
        this.list.addAll(list);
        notifyDataSetChanged();

    }

    public String getNewUpdateTime(){
        if(list != null && list.size() > 0){
            return list.get(0).getTimeSort();//返回
        }else
            return "1";
    }

    public String getOldUpdateTime(){
        if(list != null && list.size() >0){
            return list.get(list.size() - 1).getTimeSort();
        }else
            return "1";//这个是最小时间
    }


    public void saveCircleToFile(String fileName){
        //只是保存十条数据到文件缓存中
        List<News> saveList ;
        if(this.list.size() > 10)
            saveList = this.list.subList(0,10);
        else
            saveList = this.list;

        try {
            if(FileUtil.createFile(fileName))
            {
                FileUtil.writeNewsToFile(saveList,fileName);
                Log.d("--------->", "保存成功");
                LogToSd.debug("资讯保存文件,保存成功!");
            }
            else
            {
                Log.e("--------->","无法创建文件");
                LogToSd.error("资讯保存文件,错误"+"无法创建文件夹");
            }
        } catch (Exception e) {
            Log.e("--------->","无法找到文件");
            LogToSd.error("资讯保存文件,错误："+e.getMessage().toString());
        }
    }





    @Override
    public void onScrollStateChanged(AbsListView view, int scrollState) {

        switch(scrollState) {
            case AbsListView.OnScrollListener.SCROLL_STATE_IDLE: //滑动后静止

                isScroll = false;
                this.listView.ToEnd(view);
                break;
            case AbsListView.OnScrollListener.SCROLL_STATE_FLING://手指离开后惯性滑动

                isScroll = true;
                this.listView.ToEnd(view);
                break;
            case AbsListView.OnScrollListener.SCROLL_STATE_TOUCH_SCROLL://手指一直在滑动

                isScroll = true;
                this.listView.ToEnd(view);
                break;
        }

    }

    @Override
    public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
        listView.setFirstItemIndex(firstVisibleItem);
    }


}
